﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Class for the shareIcon Movieclip
**/

package com.alexrindo.flashview.UI.share 
{
	import com.alexrindo.flashview.data.GalleryData;
	import com.alexrindo.flashview.image.ImageManager;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	public class ShareIcon extends MovieClip
	{
		private var _name:String
		private var _shareString:String
		private var _shareURL:URLRequest
		private var _shareIconLoader:Loader
		
		private var _data:GalleryData
		private var _images:ImageManager
		
		public function ShareIcon(name:String)
		{
			_name = name
			shareName.text = _name
			
			_data = GalleryData.getInstance()
			_images = ImageManager.getInstance()
			
			addListeners()
			loadIcon()
		}
		
		private function addListeners():void
		{
			addEventListener(MouseEvent.CLICK, onShareIconClick)
			addEventListener(MouseEvent.ROLL_OVER, onShareIconOver)
			addEventListener(MouseEvent.ROLL_OUT, onShareIconOut)
		}
		
		private function loadIcon():void
		{
			_shareIconLoader = new Loader()
			_shareIconLoader.load(new URLRequest("icons/" + _name + ".png"))
			_shareIconLoader.x = 20
			_shareIconLoader.y = 10
			_shareIconLoader.alpha = 0
			addChildAt(_shareIconLoader, 2)
		}
		
		private function onShareIconClick(e:MouseEvent):void 
		{
			switch (_name) 
			{
				case "Twitter":
					_shareString = "http://twitter.com/home?status=" + _data.getName(_images.index) + ": " + _data.baseURL + _images.getCurrentImageURL()
				break
				
				case "Digg":
					_shareString = "http://digg.com/submit?phase=2&url=" + _data.baseURL + _images.getCurrentImageURL() + "&title=" + _data.getName(_images.index) + "&bodytext=" + 							  _data.getCaption(_images.index) + "&media=image"
				break
				
				case "Reddit":
					_shareString = "http://www.reddit.com/submit?url=" + _data.baseURL + _images.getCurrentImageURL() + "&title=" + _data.getName(_images.index)
				break
				
				case "Facebook":
					_shareString = "http://www.facebook.com/sharer.php?u=" + _data.baseURL + _images.getCurrentImageURL() + "&t=" + _data.getName(_images.index)
				break
				
				case "Stumbleupon":
					_shareString = "http://www.stumbleupon.com/submit?url=" + _data.baseURL + _images.getCurrentImageURL() + "&title=" + _data.getName(_images.index)
				break
				
				case "Delicious":
					_shareString = "http://delicious.com/post?url=" + _data.baseURL + _images.getCurrentImageURL() + "&title=" + _data.getName(_images.index) + "&notes=" + 							  _data.getCaption(_images.index)
				break
			}
			
			_shareURL = new URLRequest(_shareString)
			navigateToURL(_shareURL,"_blank")
		}
		
		private function onShareIconOver(e:MouseEvent):void 
		{
			TweenLite.to(shareName, 0.3, {y:-8, ease:Circ.easeOut});
            TweenLite.to(_shareIconLoader, 0.3, {y:23, alpha:1, ease:Circ.easeOut});
		}
		
		private function onShareIconOut(e:MouseEvent):void 
		{
			TweenLite.to(shareName, 0.3, {y:15, ease:Circ.easeOut});
            TweenLite.to(_shareIconLoader, 0.3, {y:10, alpha:0, ease:Circ.easeOut});
		}
		
		
		
	}

}